package it.unibo.mycruise.chief.presentation.swing;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import it.unibo.convoycruisecontrol.chiefboatsubsystem.ControlPanel;
import it.unibo.convoycruisecontrol.chiefboatsubsystem.IControlPanel;
import it.unibo.measures.KnotsToMetersSecond;
import it.unibo.mycruise.boat.Dashboard;
import it.unibo.mycruise.boat.presentation.swing.DashboardView;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ControlPanelView extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton startButton = null;
	private JButton stopButton = null;
	private JTextField expressionTextField = null;

	private JButton sendButton = null;

	private ControlPanelController controlPanelController = null;

	public ControlPanelView(ControlPanelController controlPanelController) {
		super();
		this.controlPanelController = controlPanelController;
		initGui();
	}

	private void initGui() {
		setLayout(new FlowLayout());

		startButton = new JButton("START");
		startButton.setPreferredSize(new Dimension(70,30));
		startButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("botton start pressed:");
				controlPanelController.startConvoy();
			}
		});

		stopButton = new JButton("STOP");
		stopButton.setPreferredSize(new Dimension(70,30));
		stopButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("botton stop pressed:");
				controlPanelController.stopConvoy();
			}
		});

		sendButton = new JButton("SEND");
		sendButton.setPreferredSize(new Dimension(70,30));
		sendButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(expressionTextField.getText() != "")
				{
					String expression = expressionTextField.getText();
					System.out.println("botton send pressed: " + expression);
					controlPanelController.setConvoySpeed((float) KnotsToMetersSecond.convert(Double.parseDouble(expression)));
				}
			}
		});

		expressionTextField = new JTextField(42);
		expressionTextField.setPreferredSize(new Dimension(70,30));

		add(expressionTextField);
		add(new JLabel("kn"));
		add(sendButton);
		add(startButton);
		add(stopButton);

		setVisible(true);	
	}
}
